package com.wn.class30.tools;

import com.wn.class30.entity.Systemmenu;


import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Description: MenuTree 方法是
 * @author: jzy
 * @date: 2020/3/11 6:23 PM
 */
public class MenuTree {

    public static List<Systemmenu> getMenuTree(Collection<Systemmenu> menus){
        List<Systemmenu> menuTree = new ArrayList<>();
        //遍历set集合，取得一级菜单
        for (Systemmenu menu : menus) {
            if (menu.getMenuLevel() == 1) {
                menuTree.add(menu);
            }
        }
        //取得二级菜单
        for (Systemmenu menu : menus) {
            for (int i = 0; i < menuTree.size(); i++) {
                if (menu.getMenuLevel() == 2 && menu.getParentid() == menuTree.get(i).getMenuId()) {
                    menuTree.get(i).getChildrens().add(menu);
                }
            }
        }
        //取得三级菜单
        for (Systemmenu menu : menus) {
            for (Systemmenu menu1 : menuTree) {
                List<Systemmenu> childerns = menu1.getChildrens();
                for (Systemmenu childern : childerns) {
                    if (menu.getMenuLevel() == 3 && menu.getParentid() == childern.getMenuId()) {
                        childern.getChildrens().add(menu);
                    }
                }
            }
        }

        return menuTree;
    }
}